package com.aichen.starter.code.service;

import com.aichen.starter.code.util.JavaMailSenderUtil;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Date;

/**
 * @author aichen
 * @date 2022/7/5 14:55
 */
public class AichenEmailCodeSend {

    private String appName;

    public AichenEmailCodeSend(String appName){
        this.appName = appName;
    }

    /**
     * 发送验证码
     * @param email 发送目标
     * @param code 验证码
     */
    public void send(String email, String code){
        JavaMailSender javaMailSender = JavaMailSenderUtil.getJavaMailSender();
        SimpleMailMessage message = new SimpleMailMessage();
        //设置邮件主题
        message.setSubject(appName + ": 验证码");
        //设置邮件发送者
        message.setFrom("1589054512@qq.com");
        //设置邮件接收者，可以有多个接收者
        message.setTo(email);
        //设置邮件发送日期
        message.setSentDate(new Date());
        //设置邮件的正文
        message.setText("验证码：" + code);
        javaMailSender.send(message);
    }
}
